/**
 * Created by lgc on 2017/10/28.
 */
import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as getters from './getters'
import home from './modules/home'
import login from './modules/login'
import * as types from './mutation-types'

Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

export default new Vuex.Store({
  state: {
    user: {},
    token: null,
    title: '',
    tagsList:[],
    menuList:[]
  },
  actions,
  getters,
  modules: {
    home,
    login
  },
  mutations: {
    setTagsList (state, list) {
      console.info(list);
      state.tagsList.push(...list);
    },
    updateMenulist (state,appRouter) {
      let menuList = [];
      appRouter.forEach((item, index) => {
        if (item.access !== undefined) {
            if (item.children.length <= 1) {
              menuList.push(item);
            } else {
              let i = menuList.push(item);
              let childrenArr = [];
              childrenArr = item.children.filter(child => {
                if (child.access !== undefined) {
                  if (child.access === accessCode) {
                    return child;
                  }
                } else {
                  return child;
                }
              });
              menuList[i - 1].children = childrenArr;
            }
        } else {
          if (item.children.length <= 1) {
            menuList.push(item);
          } else {
            let i = menuList.push(item);
            let childrenArr = [];
            childrenArr = item.children.filter(child => {
              if (child.access !== undefined) {
                  return child;
              } else {
                return child;
              }
            });
            menuList[i - 1].children = childrenArr;
          }
        }
      });
      console.info(menuList);
      state.menuList = menuList;
    },
    [types.LOGIN]: (state, data) => {
      localStorage.token = data;
      state.token = data;
      console.info(state.token);
    },
    [types.LOGOUT]: (state) => {
      localStorage.removeItem('token');
      state.token = null
    },
    [types.TITLE]: (state, data) => {
      state.title = data;
    },
    setAvator (state, path) {
      localStorage.avatorImgPath = path;
    }
  },
  strict: debug
})
